package com.qingke.pms.command.user;

import java.util.List;

import com.qingke.pms.command.UserCommand;
import com.qingke.pms.pojo.Project;
import com.qingke.pms.pojo.User;
import com.qingke.pms.util.PMSConsole;

public class ListProjectsCommand extends UserCommand {

	public ListProjectsCommand(User user) {
		super(user);
	}

	@Override
	public void execute() {
		while(true){
			PMSConsole.println("1.all 2.id 3.code(like) 4.name(like)");
			PMSConsole.println("5.construction unit id");
			PMSConsole.println("6.first party id");
			PMSConsole.println("7.exit");
			String cmd=PMSConsole.promptNotEmpty("list project>").replaceAll(" ","").trim().toLowerCase();
			switch(cmd){
			case "1":
			case "all":
				all();
				continue;
			case "2":
			case "id":
				id();
				continue;
			case "3":
			case "code":
				code();
				continue;
			case "4":
			case "name":
				name();
				continue;
			case "5":
			case "construction unit id":
				constructionUnitId();
				continue;
			case "6":
			case "first party id":
				firstPartyId();
				continue;
			case "7":
			case "exit":
				break;
			default:
				PMSConsole.println("err input.");
				continue;
			}
			break;
		}
	}
	
	private void all(){
		List<Project> list=dao.selectAllProjects();
		if(list.size()==0){
			PMSConsole.println("no project");
			return ;
		}
		PMSConsole.println("-------all projects-------");
		for(Project p:list){
			PMSConsole.println(p);
		}
		PMSConsole.println();
	}
	
	private void id(){
		int id=PMSConsole.getInt("project id:");
		if(!dao.isProjectExistById(id)){
			PMSConsole.println("the project is not existed.");
			return ;
		}
		Project project=dao.selectProjectById(id);
		PMSConsole.println(project);
	}

	private void code(){
		String code=PMSConsole.promptCanEmpty("code like:");
		String codeLike="%"+code+"%";
		List<Project> list=dao.selectProjectsByLikeCode(codeLike);
		if(list.size()==0){
			PMSConsole.println("no result");
			return ;
		}
		PMSConsole.println("-------projects code like:"+code+"-------");
		for(Project p:list){
			PMSConsole.println(p);
		}
		PMSConsole.println();
	}
	
	private void name(){
		String name=PMSConsole.promptCanEmpty("name like:");
		String nameLike="%"+name+"%";
		List<Project> list=dao.selectProjectsByLikeName(nameLike);
		if(list.size()==0){
			PMSConsole.println("no result");
			return ;
		}
		PMSConsole.println("-------projects name like:"+name+"-------");
		for(Project p:list){
			PMSConsole.println(p);
		}
		PMSConsole.println();
	}
	
	private void constructionUnitId(){
		int id=PMSConsole.getInt("construction unit id:");
		if(!dao.isCompanyExistById(id)){
			PMSConsole.println("the company is not existed.");
			return;
		}
		List<Project> list=dao.selectProjectsByConstructionUnitId(id);
		if(list.size()==0){
			PMSConsole.println("no result");
			return ;
		}
		PMSConsole.println("-------projects construction unit id:"+id+"-------");
		for(Project p:list){
			PMSConsole.println(p);
		}
		PMSConsole.println();
	}
	
	private void firstPartyId(){
		int id=PMSConsole.getInt("first party id:");
		if(!dao.isCompanyExistById(id)){
			PMSConsole.println("the company is not existed.");
			return;
		}
		List<Project> list=dao.selectProjectsByFirstPartyId(id);
		if(list.size()==0){
			PMSConsole.println("no result");
			return ;
		}
		PMSConsole.println("-------projects first party id:"+id+"-------");
		for(Project p:list){
			PMSConsole.println(p);
		}
		PMSConsole.println();
	}

}
